package org.lql.algo.codecrush.hot100.stack;

import java.util.Arrays;
import java.util.Stack;

/**
 * @author: liangqinglong
 * @date: 2025-10-14 11:28
 * @description: 739. 每日温度 <a href="https://leetcode.cn/problems/daily-temperatures/description/?envType=study-plan-v2&envId=top-100-liked">...</a>
 **/
public class DailyTemperatures {

	public int[] dailyTemperatures(int[] temperatures) {
		int length = temperatures.length;
		int[] ans = new int[length];
		Stack<Integer> stack = new Stack<>();
		for (int i = 0; i < length; i++) {
			while (!stack.isEmpty() && temperatures[stack.peek()] < temperatures[i]) {
				Integer prevIndex = stack.pop();
				ans[prevIndex] = i - prevIndex;
			}
			stack.push(i);
		}
		return ans;
	}

	public static void main(String[] args) {
		int[] temperatures = {73, 74, 75, 71, 69, 72, 76, 73};
		DailyTemperatures dailyTemperatures = new DailyTemperatures();
		System.out.println(Arrays.toString(dailyTemperatures.dailyTemperatures(temperatures)));
	}
}
